#define  _CRT_SECURE_NO_WARNINGS
class Solution
{
public:
    vector<int> getLeastNumbers(vector<int>& nums, int k)
    {
        srand(time(NULL));
        qsort(nums, 0, nums.size() - 1, k);
        return { nums.begin(), nums.begin() + k };
    }
    void qsort(vector<int>& nums, int l, int r, int k)
    {
        if (l >= r) return;
        
        int key = getRandom(nums, l, r);
        int left = l - 1, right = r + 1, i = l;
        while (i < right)
        {
            if (nums[i] < key) swap(nums[++left], nums[i++]);
            else if (nums[i] == key) i++;
            else swap(nums[--right], nums[i]);
        }
        

        int a = left - l + 1, b = right - left - 1;
        if (a > k) qsort(nums, l, left, k);
        else if (a + b >= k) return;
        else qsort(nums, right, r, k - a - b);
    }
    int getRandom(vector<int>& nums, int l, int r)
    {
        return nums[rand() % (r - l + 1) + l];
    }
};